﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using _MetalSlug.MyCharacters.Characters;
using System.Xml;
using _MetalSlug.Weapons.Units;
using _MetalSlug.MyCharacters.Characters.Guns;

namespace _MetalSlug.Mementoes
{
    public class HeroMemento: IMemento
    {
        int hp;
        public int Hp
        {
            get { return hp; }
        }

        String name;
        public String Name
        {
            get { return name; }
        }

        Vector2 topLeft;
        public Vector2 TopLeft
        {
            get { return topLeft; }
        }

        MyCharacter.CURRENT_ACTIVITY currentActivity;
        public MyCharacter.CURRENT_ACTIVITY CurrentActivity
        {
            get { return currentActivity; }
        }

        MyCharacter.RESOURCE_FACE currentFacing;
        public MyCharacter.RESOURCE_FACE CurrentFacing
        {
            get { return currentFacing; }
        }

        int indexBody;
        public int IndexBody
        {
            get { return indexBody; }
        }

        int indexFoot;
        public int IndexFoot
        {
            get { return indexFoot; }
        }

        List<BulletMemento> lstBullet;
        List<GunMemento> lstGun;

        Gun.TYPE currentGun;
        public Gun.TYPE CurrentGun
        {
            get { return currentGun; }
        }

        public HeroMemento(MyCharacter RepresentHero)
        {
            name = RepresentHero.Name;
            hp = RepresentHero.Hp;
            topLeft = RepresentHero.TopLeft;
            currentActivity = RepresentHero.Activity;
            currentFacing = RepresentHero.Facing;
            indexBody = RepresentHero.MyDisplayer.Body.ITexture;
            indexFoot = RepresentHero.MyDisplayer.Foot.ITexture;
            lstBullet = new List<BulletMemento>();
            foreach (Bullet bullet in RepresentHero.GetListBullet())
            {
                BulletMemento bulletMemento = new BulletMemento(bullet);
                lstBullet.Add(bulletMemento);
            }

            lstGun = new List<GunMemento>();
            foreach(Gun gun in RepresentHero.GetListGun())
            {
                GunMemento gunMemento = new GunMemento(gun);
                lstGun.Add(gunMemento);
            }

            currentGun = RepresentHero.CurrentGun.TypeOfGun;
        }

        public HeroMemento(XmlElement Element)
        {
            name = Element.GetAttribute("Name");
            currentGun =(Gun.TYPE) Enum.Parse(typeof(Gun.TYPE),Element.GetAttribute("Gun"));
            hp = int.Parse(Element.GetAttribute("Hp"));
            currentActivity = (MyCharacter.CURRENT_ACTIVITY)Enum.Parse(typeof(MyCharacter.CURRENT_ACTIVITY), Element.GetAttribute("Activity"));
            currentFacing = (MyCharacter.RESOURCE_FACE)Enum.Parse(typeof(MyCharacter.RESOURCE_FACE), Element.GetAttribute("Facing"));

            float X, Y;
            X = float.Parse(Element.GetAttribute("X"));
            Y = float.Parse(Element.GetAttribute("Y"));
            topLeft = new Vector2(X, Y);

            indexBody = int.Parse(Element.GetAttribute("IndexBody"));
            indexFoot = int.Parse(Element.GetAttribute("IndexFoot"));

            lstBullet = new List<BulletMemento>();
            XmlNodeList Bullets = Element.SelectNodes("./Bullet");
            foreach(XmlElement Child in Bullets)
            {
                BulletMemento bullet = new BulletMemento(Child);
                lstBullet.Add(bullet);
            }

            lstGun = new List<GunMemento>();
            XmlNodeList Guns = Element.SelectNodes("./Gun");
            foreach (XmlElement Child in Guns)
            {
                GunMemento gun = new GunMemento(Child);
                lstGun.Add(gun);
            }
        }

        public XmlElement ToXML(XmlDocument Doc)
        {
            XmlElement Element = Doc.CreateElement("Character");
            Element.SetAttribute("Name", Name.ToString());
            Element.SetAttribute("Gun",CurrentGun.ToString());
            Element.SetAttribute("Hp", Hp.ToString());
            Element.SetAttribute("Activity", CurrentActivity.ToString());
            Element.SetAttribute("Facing", CurrentFacing.ToString());
            Element.SetAttribute("X", TopLeft.X.ToString());
            Element.SetAttribute("Y", TopLeft.Y.ToString());
            Element.SetAttribute("IndexBody", IndexBody.ToString());
            Element.SetAttribute("IndexFoot", IndexFoot.ToString());
            
            foreach(BulletMemento bullet in lstBullet)
            {
                Element.AppendChild(bullet.ToXML(Doc));
            }

            foreach (GunMemento gun in lstGun)
            {
                Element.AppendChild(gun.ToXML(Doc));
            }

            return Element;
        }

        public List<BulletMemento> GetListBullet()
        {
            return lstBullet;
        }

        public List<GunMemento> GetListGun()
        {
            return lstGun;
        }
    }
}
